from app import db
from datetime import datetime

class CheckinRecord(db.Model):
    """入场登记记录表"""
    __tablename__ = 'checkin_records'
    
    id = db.Column(db.Integer, primary_key=True)
    order_id = db.Column(db.Integer, db.ForeignKey('orders.id'), nullable=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'), nullable=False)
    time_slot_id = db.Column(db.Integer, db.ForeignKey('venue_time_slots.id'), nullable=False)
    
    # 预约信息
    reservation_date = db.Column(db.Date, nullable=False)
    participant_count = db.Column(db.Integer, default=1, comment='预约参与人数')
    
    # 入场信息
    checkin_time = db.Column(db.DateTime, comment='入场时间')
    checkout_time = db.Column(db.DateTime, comment='出场时间')
    actual_participants = db.Column(db.Integer, default=0, comment='实际到场人数')
    
    # 状态
    status = db.Column(db.SmallInteger, default=0, comment='状态: 0-未入场, 1-已入场, 2-已出场, 3-超时未入场')
    
    # 备注
    checkin_note = db.Column(db.Text, comment='入场备注')
    checkout_note = db.Column(db.Text, comment='出场备注')
    
    # 时间戳
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 关联关系
    order = db.relationship('Order', backref='checkin_records')
    user = db.relationship('User', backref='checkin_records')
    venue = db.relationship('Venue', backref='checkin_records')
    time_slot = db.relationship('VenueTimeSlot', backref='checkin_records')
    
    def to_dict(self):
        return {
            'id': self.id,
            'order_id': self.order_id,
            'user_id': self.user_id,
            'venue_id': self.venue_id,
            'time_slot_id': self.time_slot_id,
            'reservation_date': self.reservation_date.isoformat() if self.reservation_date else None,
            'participant_count': self.participant_count,
            'checkin_time': self.checkin_time.isoformat() if self.checkin_time else None,
            'checkout_time': self.checkout_time.isoformat() if self.checkout_time else None,
            'actual_participants': self.actual_participants,
            'status': self.status,
            'status_text': self.get_status_text(),
            'checkin_note': self.checkin_note,
            'checkout_note': self.checkout_note,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }
    
    def get_status_text(self):
        status_map = {
            0: '未入场',
            1: '已入场',
            2: '已出场',
            3: '超时未入场'
        }
        return status_map.get(self.status, '未知状态')
    
    @classmethod
    def create_from_order(cls, order):
        """从订单创建入场记录"""
        # 确保reservation_date是date对象
        reservation_date = order.reservation_date
        if isinstance(reservation_date, str):
            from datetime import datetime
            reservation_date = datetime.strptime(reservation_date, '%Y-%m-%d').date()
        
        return cls(
            order_id=order.id,
            user_id=order.user_id,
            venue_id=order.venue_id,
            time_slot_id=order.time_slot_id,
            reservation_date=reservation_date,
            participant_count=order.participant_count or 1,
            status=0  # 未入场
        ) 